package vn.com.vega.chacha.v2.fragment;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.ActivityBase;
import vn.com.vega.chacha.v2.Appinstant;
import vn.com.vega.chacha.v2.MainScreen;
import vn.com.vega.chacha.v2.VideoPlayerScreen;
import vn.com.vega.music.clientserver.JsonVideo;
import vn.com.vega.music.objects.Video;
import vn.com.vega.music.utils.Const;
import vn.com.vega.music.utils.VegaLog;
import vn.com.vega.music.view.adapter.SongListAdapter;
import vn.com.vega.music.view.adapter.VideoListAdapter;
import vn.com.vega.music.view.adapter.base.AutoLoadingAdapter;
import vn.com.vega.music.view.adapter.base.AutoLoadingListener;
import vn.com.vega.music.view.custom.LoadingView;
import vn.com.vega.music.view.custom.LoadingView.RetryListener;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

public class ListVideoFeatureFragment extends Fragment implements
		AutoLoadingListener, Const {
	private MainScreen mMainScreen;
	private Appinstant mApp;
	private VideoListAdapter mAdapter;
	private ListView mListVideo;
	// private RelativeLayout mLayoutLoading;
	// private ProgressBar mLoadingbar;
	// private TextView mLoadingText;
	// private Button mRetryBtn;
	private LoadVideoFeature mLoader;
	private LoadingView mLoadingView;

	public static Fragment newInstance() {
		ListVideoFeatureFragment fragment = new ListVideoFeatureFragment();
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View root = inflater.inflate(R.layout.list_song_feature, null);
		mListVideo = (ListView) root.findViewById(R.id.lv_list_song);
		mLoadingView = (LoadingView) root
				.findViewById(R.id.loadingView_loading_list_feature);
		mLoadingView.setRetryListener(retryListener);
		return root;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mMainScreen = (MainScreen) getActivity();
		mApp = (Appinstant) mMainScreen.getApplicationContext();
		// if (mApp.listSongFeature.size() > 0) {
		setAdapter();
		// }
	}

	private void setAdapter() {
		mAdapter = new VideoListAdapter(mMainScreen, mApp.listVideoFeature,
				false);
		mListVideo.setAdapter(new AutoLoadingAdapter(mAdapter, this));
		mListVideo.setOnItemClickListener(onVideoClickListener);
	}

	@Override
	public Activity getContext() {
		// TODO Auto-generated method stub
		return mMainScreen;
	}

	@Override
	public List<Object> getMoreData() {
		// TODO Auto-generated method stub
		if (mAdapter.getCount() >= LIST_ITEM_LIMIT) {
			return null;
		}
		List<Object> result = new ArrayList<Object>();
		JsonVideo jav = JsonVideo.loadTopVideoList(mAdapter.getCount());
		if (jav.isSuccess()) {
			result.addAll(jav.videos);
		}
		return result;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		VegaLog.e(SongListAdapter.class.getName(), "Resume list feature video");
		if (mAdapter.getCount() == 0) {
			loadListVideoFeature();
		} else {
			showListVideo();
		}
	}

	private void loadListVideoFeature() {
		if (mLoader != null && mLoader.getStatus() == Status.RUNNING) {
			showLoading();
		} else {
			if (mLoader != null) {
				mLoader.cancel(true);
			}
			mLoader = new LoadVideoFeature(this);
			mLoader.execute();
		}
	}

	private void showLoading() {
		mListVideo.setVisibility(View.GONE);
		mLoadingView.startLoading();
	}

	private void showListVideo() {
		if (mAdapter.getCount() == 0) {
			mListVideo.setVisibility(View.GONE);
			mLoadingView.loadingFinish(false, "Danh sách video trống");
		} else {
			mLoadingView.loadingFinish(true, null);
			mListVideo.setVisibility(View.VISIBLE);
		}
	}

	RetryListener retryListener = new RetryListener() {

		@Override
		public void onRetry() {
			// TODO Auto-generated method stub
			loadListVideoFeature();
		}
	};

	View.OnClickListener retryClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			loadListVideoFeature();
		}
	};

	AdapterView.OnItemClickListener onVideoClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			String listVideonameString = ListVideoFeatureFragment.class
					.getName();
			List<Video> listVideos = new ArrayList<Video>();
			for (int i = 0; i < mApp.listVideoFeature.size(); i++) {
				listVideos.add((Video) mApp.listVideoFeature.get(i));
			}
			mApp.tranportListVideo.put(listVideonameString, listVideos);
			Intent intent = new Intent(mMainScreen, VideoPlayerScreen.class);
			intent.putExtra(VideoPlayerScreen.INDEX_VIDEO, position);
			intent.putExtra(VideoPlayerScreen.VIDEO_LIST_NAME,
					listVideonameString);
			mMainScreen.startActivity(intent);
		}
	};

	static class LoadVideoFeature extends AsyncTask<Void, Void, Void> {
		WeakReference<ListVideoFeatureFragment> listSongFragmentRfc;

		public LoadVideoFeature(ListVideoFeatureFragment base) {
			listSongFragmentRfc = new WeakReference<ListVideoFeatureFragment>(
					base);
		}

		private boolean checkActIsLive() {
			ListVideoFeatureFragment fragment = listSongFragmentRfc.get();
			if (fragment == null) {
				return false;
			}
			if (fragment.mMainScreen instanceof ActivityBase) {
				if ((fragment.mMainScreen).isDestroy) {
					return false;
				}
			}
			return true;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			if (!checkActIsLive()) {
				return null;
			}
			ListVideoFeatureFragment fragment = listSongFragmentRfc.get();
			JsonVideo jav = JsonVideo.loadTopVideoList(0);
			if (jav.isSuccess()) {
				fragment.mApp.listVideoFeature.clear();
				fragment.mApp.listVideoFeature.addAll(jav.videos);
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (isCancelled() || !checkActIsLive()) {
				return;
			}

			ListVideoFeatureFragment fragment = listSongFragmentRfc.get();
			fragment.setAdapter();
			fragment.showListVideo();
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			if (!checkActIsLive()) {
				return;
			}
			ListVideoFeatureFragment fragment = listSongFragmentRfc.get();
			fragment.showLoading();
		}
	}
	
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		mMainScreen.trackPageView("Danh sách Video chọn lọc");
	}
}
